﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Model;
using IHISCETeim.Library;
using IHISCETeim.BLL;
namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OInspectionAdd : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.新增 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "OInspectionList";
            }
        }
        protected string t
        {
            get
            {
                return GetRequestValue<string>("t", "1");
            }
        }
        protected string SourceType
        {
            get
            {
                if (t == "1") return "完检单";
                if (t == "2") return "出库检查单";
                return "完检单";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                addmenu();
                
            }
        }

        protected void addmenu()
        {
            string[] DJNGItemCodes = GetRequestValue<string>("DJNGItemCode", "").Split(new char[] { ',' });
            string[] DJNGExceptionrecords = GetRequestValue<string>("DJNGExceptionrecord", "").Split(new char[] { ',' });
            string[] PNOS = GetRequestValue<string>("PNO", "").Split(new char[] { ',' });
            string[] CodeTagS = GetRequestValue<string>("CodeTag", "").Split(new char[] { ',' });
            string[] StationS = GetRequestValue<string>("Station", "").Split(new char[] { ',' });
            string[] SupplierS = GetRequestValue<string>("Supplier", "").Split(new char[] { ',' });
            string[] NGClassS = GetRequestValue<string>("NGClass", "").Split(new char[] { ',' });
            string[] DutyWorkers = GetRequestValue<string>("DutyWorker", "").Split(new char[] { ',' });

            OInspectionInfo obj = Request.SerializableObject<OInspectionInfo>();
            obj.SourceType = SourceType;
            List<ONGInspectionInfo> ngitems = new List<ONGInspectionInfo>();

            for (int index = 0; index < DJNGExceptionrecords.Count(); index++)
            {
                ONGInspectionInfo ngitem = new ONGInspectionInfo();
                if (!string.IsNullOrEmpty(DJNGExceptionrecords[index]))
                {
                    //ngitem.ItemCode = DJNGItemCodes[index];
                    ngitem.Exceptionrecord = DJNGExceptionrecords[index];
                    //ngitem.ItemType = 0;
                    ngitem.SerialNumber = obj.SerialNumber;
                    //ngitem.PNO = PNOS[index];
                   // ngitem.CodeTag = CodeTagS[index];
                    ngitem.Station = StationS[index];
                    ngitem.Supplier = SupplierS[index];
                    ngitem.MachineType = obj.MachineType;
                    ngitem.BillDate = obj.CheckDate;
                    ngitem.SourceType = "完检单";
                    ngitem.NGClass = NGClassS[index];
                    ngitem.DutyWorker = DutyWorkers[index];
                    ngitems.Add(ngitem);
                }
            }


            this.AddLog(Permission.新增, null, obj);
            int r=OInspectionController.Add(obj, ngitems);
            switch (r)
            { 
                case -1:
                    ErrorJson("新增失败,机号已存在，不能重复添加．");
                    break;
                case -2:
                    ErrorJson("新增失败,仪表编号已存在，不能重复添加．");
                    break;
                default:
                    SuccessJson("新增成功");
                    break;
            }
           
        }
    }
}